<template>
  <div class="hello">
    <h1>传递的名称：{{ msg }}</h1>
    <h1>自己的名称：{{ selfName }}</h1>
    <button @click="setName">设置自己的名称</button>
    <button @click="setNameProp">设置传递的名称</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    msg: {
      type: String,
      required: false,
      default: "---",
    },
  },
  data() {
    return {
      selfName: 1,
    };
  },
  beforeCreate() {
    console.log("----beforeCreate----");
    // console.log(this.msg); //msg
    // console.log(this.$el); //undefined
  },
  created() {
    console.log("----created----");
    console.log(this.msg);
    console.log(this.$el);
  },
  beforeMount() {
    console.log("----beforeMount----");
    console.log(this.msg);
    console.log(this.$el);
  },
  mounted() {
    console.log("----mounted----");
    console.log(this.msg);
    console.log(this.$el);
  },
  methods: {
    setName() {
      this.selfName = "新名称-" + new Date() * 1;
    },
    setNameProp() {
      this.$emit("setMsg", "新名称-" + new Date() * 1);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
